import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

// 当往下滚动100的时候 appBar编程透明
const APPBAR_SCROLL_OFFSET = 100;

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _swiperImages = [
    'https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g2/M00/01/0C/ChMlWl0IyZ-IN-7CAAsS9P1yFxwAALA0gCrMSYACxMM027.jpg',
    'https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g2/M00/01/0C/ChMlWl0IyZ-IN-7CAAsS9P1yFxwAALA0gCrMSYACxMM027.jpg',
    'https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g2/M00/01/0C/ChMlWl0IyZ-IN-7CAAsS9P1yFxwAALA0gCrMSYACxMM027.jpg',
  ];

  double _appBarAlpha = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//      去除上方的padding 沉浸式 图片漫过状态栏
      body: Stack(
        children: <Widget>[
          MediaQuery.removePadding(
            context: context,
            removeTop: true,
            child: NotificationListener(
              onNotification: (event){
                if(event is ScrollUpdateNotification && event.depth == 0){
                  // 列表滚动的时候 才监听
                  _onScroll(event.metrics.pixels);
                }
              },
              child: ListView(
                children: <Widget>[
                  Container(
                    height: 160,
                    child: Swiper(
                      itemCount: _swiperImages.length,
                      autoplay: true,
                      itemBuilder: (BuildContext context,int index){
                        return Image.network(_swiperImages[index],fit: BoxFit.fill,);
                      },
                      pagination: SwiperPagination(),
                    ),
                  ),
                  Container(
                    height: 800,
                    child: ListTile(title: Text("哈哈哈")),
                  )
                ],
              ),
            ),
          ),

          Opacity(
            opacity: _appBarAlpha,
            child: Container(
              height: 80,
              decoration: BoxDecoration(
                color: Colors.white
              ),
              child: Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text("首页"),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  void _onScroll(double pixels) {
    double alpha = pixels/APPBAR_SCROLL_OFFSET;
    if(alpha<0){
      alpha = 0;
    }else if(alpha > 1){
      alpha = 1;
    }
    setState(() {
      _appBarAlpha = alpha;
    });
  }
}
